﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Projecturo.ProjecturoClient.Command;
using System.Windows.Input;
using Neits.UserService;
using System.Threading;
using Projecturo.ProjecturoClient.Connection;
using Projecturo.Model;

namespace Projecturo.ProjecturoClient.ViewModel.UserManagement
{
    public class UserProfileViewModel : WorkspaceViewModel
    {
        private const string UserProfileDisplayName = "User Profil";
        private UserProfil userProfil = new UserProfil();
        private string newPasswordConfirmed;
        private ViewModelBase userViewModel;

        private DelegateCommand changePasswordCommand;
        private DelegateCommand saveUserCommand;

        protected override void OnViewModelCreated()
        {
            base.OnViewModelCreated();
            LoadUserViewModel();
        }

        public override string DisplayName
        {
            get
            {
                return UserProfileDisplayName;
            }
            protected set
            {
                base.DisplayName = value;
            }
        }

        public string OldPassword
        {
            get { return this.userProfil.OldPassword; }
            set
            {
                if (this.userProfil.OldPassword == value)
                    return;
                this.userProfil.OldPassword = value;
                OnPropertyChanged("OldPassword");
            }
        }

        public string NewPassword
        {
            get { return this.userProfil.NewPassword; }
            set
            {
                if (this.userProfil.NewPassword == value)
                    return;
                this.userProfil.NewPassword = value;
                OnPropertyChanged("NewPassword");
            }
        }

        public string NewPasswordConfirmed
        {
            get { return this.newPasswordConfirmed;}
            set
            {
                if (this.newPasswordConfirmed == value)
                    return;
                this.newPasswordConfirmed = value;
                OnPropertyChanged("NewPasswordConfirmed");
            }
        }

        public ViewModelBase User
        {
            get { return this.userViewModel; }
            set
            {
                if (this.userViewModel == value)
                    return;
                this.userViewModel = value;
                OnPropertyChanged("User");
            }
        }

        public ICommand ChangePasswordCommand
        {
            get
            {
                if (this.changePasswordCommand == null)
                {
                    this.changePasswordCommand = new DelegateCommand(ChangePassword);
                }
                return this.changePasswordCommand;
            }
        }

        private void ChangePassword()
        {
            if (this.NewPassword.Equals(this.NewPasswordConfirmed, StringComparison.CurrentCulture) && this.NewPassword != this.OldPassword)
            {
                IUser currentUser = GetCurrentUser();
                currentUser.Password = this.NewPassword;
                Connector.Connectivity.Storage.Save(currentUser);
            }
        }

        public ICommand SaveUserCommand
        {
            get
            {
                if (this.saveUserCommand == null)
                {
                    this.saveUserCommand = new DelegateCommand(SaveUser);
                }
                return this.saveUserCommand;
            }
        }

        private void SaveUser()
        {
            this.userViewModel.SaveCommand.Execute(null);
            Thread.CurrentPrincipal = Connector.Connectivity.Storage.Get<User>(GetCurrentUser().Id);
        }

        private IUser GetCurrentUser()
        {
            IUser user = Thread.CurrentPrincipal as IUser;

            IUser loadedUser = Connector.Connectivity.Storage.Get<User>(user.Id);

            return loadedUser;
        }

        private void LoadUserViewModel()
        {
            userViewModel = ViewModelManager.Instance.CreateViewModel(typeof(User), this);
            userViewModel.SetModel(Thread.CurrentPrincipal as User);
        }
    }
}
